<template>
  <div class="userCenterContainer">
    <div class="goHome">
      <router-link to="/home" >返回首页</router-link></router-link>
    </div>
    <el-row class="avatarContainer">
      <el-col :lg="14" :sm="16" :xs="20">
        <el-row class="title">
          <div class="el-icon-arrow-right">我的头像</div>
        </el-row>

        <el-row class="avatar">
          <el-col :md="12" :xs="24" class="leftPart">
            <el-avatar :size="100" :src="userInfo.imageUrl+Math.random()" @error="true" fit="cover" >
              <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
            </el-avatar>
            <div class="text">当前头像</div>
          </el-col>

          <el-col :md="12" :xs="24" class="rightPart">
            <input class="file" name="file" type="file" accept="image/png,image/gif,image/jpeg" @change="update($event)"
                    placeholder="添加头像" 
            />
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <el-row class="userInfoContainer">
      <el-col :lg="14" :sm="16" :xs="20">
        <el-row class="title">
          <div class="el-icon-arrow-right">我的信息</div>
        </el-row>

        <el-row style="padding:0 30px">
          <el-form
            ref="userInfo"
            :model="userInfo"
            label-width="80px"
            :rules="rules"
          >

            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="userInfo.nickName"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="userInfo.gender">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
                <el-radio label="保密"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="手机号" prop="phoneNumber">
              <el-input v-model="userInfo.phoneNumber" disabled></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('userInfo')"
                >保存</el-button
              >
            </el-form-item>
          </el-form>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      file: {},
      rules: {
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" },
        ],
      },
      isLoading: false,
    };
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
    }),
  },
  methods: {
    //更新用户信息
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          console.log(this.userInfo);
          try {
            await this.$store.dispatch("updateUserInfo", this.userInfo);
            await this.$store.dispatch("getUserInfo");
          } catch (error) {
            console.log(error);
          }
        } else {
          return false;
        }
      });
    },
    //通过file类型的input框接受图片，同时上传至服务端
    async update(e) {
      let file = e.target.files[0];
      let formData = new FormData(); //创建form对象

      formData.append("avatar", file); //通过append向form对象添加数据
      formData.append("_id",this.userInfo._id)

      try {
        await this.$store.dispatch('uploadAvatar',formData)
        await this.$store.dispatch("getUserInfo");
        //强制重新渲染页面
        this.$forceUpdate();
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less">
@media screen and (max-width:410px) {
  .el-radio{
    margin-top:10px;
  }
}
@media screen and (max-width:992px) {
  .userCenterContainer .avatarContainer .rightPart{
    height: 60px;
  }
}
@media screen and (min-width:992px) {
  .userCenterContainer .avatarContainer .rightPart{
    height: 135px;
  }
}
.userCenterContainer {
  .goHome{
    height: 45px;
    line-height: 45px;
    margin-left: 20px;
    box-sizing: border-box;
    a{
      text-decoration: none;
      color: #00a1d6;
    } 
  }
  .title {
    height: 45px;
    padding: 0 15px;
    margin: 15px 0;
    box-sizing: border-box;
    line-height: 45px;
    color: #00a1d6;
    border-bottom: 1px solid gray;
  }
  
  .avatarContainer {
    display: flex;
    justify-content: center;
    .avatar {
      margin-top: 30px;
      .leftPart{
        display: flex;
        flex-direction: column;
        align-items: center;
        .text{
          margin-top: 15px;
          text-align: center;
        }
      }
      :deep(img){
        width: 100px;
      }
      .rightPart{
        display: flex;
        justify-content: center;
        align-items: center;
        input{
          width:180px
        }
      }
    }
  }

  .userInfoContainer {
    display: flex;
    justify-content: center;
    .el-form {
      margin-top: 30px;
    }
    .el-button {
      margin-top: 20px;
      width: 120px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  }
}
</style>